package logistics;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
//import java.awt.FlowLayout;
//import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;

import jdk.internal.org.objectweb.asm.util.Textifiable;
import listening_function.show_data;
import listening_function.visit_data;

public class goods_apply extends JPanel {
  private JTable table;
    private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
//	private StudentC stuControl = new StudentC();


	/**
	 * Create the panel.
	 */
	public goods_apply() {
		this.setSize(750, 410);
		JScrollPane scrollPane = new JScrollPane();
		
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
			},
			new String[] {
				"\u7F16\u53F7", "\u7533\u8BF7\u65F6\u95F4", "\u7533\u8BF7\u4EBA", "\u540D\u79F0", "\u6570\u91CF", "\u72B6\u6001", "\u7533\u8BF7\u7406\u7531"
			}
		));
		table.getColumnModel().getColumn(1).setPreferredWidth(86);
		table.getColumnModel().getColumn(6).setPreferredWidth(340);
		table.getColumnModel().getColumn(6).setMinWidth(22);
		scrollPane.setViewportView(table);

		new show_data("table1", table);
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String str= null;
			
				if("".equals(textField.getText())||"".equals(textField_1.getText())||
						"".equals(textField_2.getText())||"".equals(textField_3.getText())
						||"".equals(textField_4.getText())){
					showMessage("添加失败！信息不能为空！");
					return;
			
				}else{
					showMessage("添加成功！");
					add1();  
					clear();
				}
				
				
				
			}
		});

		JButton button = new JButton("删除");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] row = table.getSelectedRows();
				for(int i = row[0] ; i<= row[row.length-1]; i++){
					String id=(String) table.getValueAt(i, 0);
				
					String sql = "delete from table1 where numbe='"+id+"'";
					new visit_data(sql);
					
				}
				newData();

			}
		});

		JButton button_1 = new JButton("查找");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				look();
			}
		});

		JLabel lblNewLabel = new JLabel("\u7533\u8BF7\u6570\u91CF\uFF1A");

		textField = new JTextField();
		textField.setColumns(10);

		JLabel label = new JLabel("\u7533\u8BF7\u65F6\u95F4\uFF1A");

		JLabel label_1 = new JLabel("\u7533\u8BF7\u4EBA\uFF1A");

		JLabel label_2 = new JLabel("\u7533\u8BF7\u7406\u7531\uFF1A");
		
		JLabel label_3 = new JLabel("\u7533\u8BF7\u7269\u54C1\uFF1A");
		
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);

		textField_2 = new JTextField();
		textField_2.setColumns(10);

		textField_3 = new JTextField();
		textField_3.setColumns(10);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		
		JButton button_2 = new JButton("刷新");
		button_2.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				new show_data("table1", table);
			};		
		});
		
		JButton button_4 = new JButton("重置");
		button_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				clear();
			}
		});
		
		JLabel label_4 = new JLabel("\u7F16\u53F7\uFF1A");
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		
		
		  
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(label_3, GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(lblNewLabel)
							.addGap(2)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(label)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
							.addGap(6)
							.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
							.addGap(20))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(8)
									.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 647, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
										.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
										.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
										.addComponent(button, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
										.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)))
								.addGroup(groupLayout.createSequentialGroup()
									.addContainerGap()
									.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 370, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(label_1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)))
							.addGap(6)))
					.addGap(2))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 293, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton)
							.addGap(18)
							.addComponent(button)
							.addGap(63)
							.addComponent(button_1)
							.addGap(18)
							.addComponent(button_4)
							.addGap(28)
							.addComponent(button_2)))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(6)
									.addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addComponent(lblNewLabel)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_3))
							.addGap(18))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(3)
									.addComponent(label_4))
								.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_2)
						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(20))
		);
		setLayout(groupLayout);

		
	}

//	protected biaoneixinxi getData() {
//		biaoneixinxi xx = null;
//		String NUMBE=textField.getText();
//		String A_DATE=textField_1.getText();
//		String APPLICANT=textField_2.getText();
//		String R_APPLICANT=textField_3.getText();
//
////		xx = new biaoneixinxi(NUMBE, A_DATE, APPLICANT, R_APPLICANT);
//		return xx;
//	}

	protected void clear() {
		textField.setText("");
		textField_1.setText("");
		textField_2.setText("");
		textField_3.setText("");
		textField_4.setText("");
	}

	private void showMessage(String str) {
		JOptionPane.showMessageDialog(null, str, "消息提示",
				JOptionPane.INFORMATION_MESSAGE);
	}
	/**
	 * 获取数据库中的信息
	 */

//	private void getStudents() {
//		List<biaoneixinxi> students = null;
////		students = stuControl.getAll();
//		StuinfoTableM model = new StuinfoTableM(students);
//		table.setModel(model);
//	}
	public void add1(){
		String APPLICANT = textField_1.getText();
		String  MUCH= textField.getText();
		String R_APPLICANT = textField_2.getText();
		String A_DATE = textField_3.getText();
		String NAME = textField_4.getText();
		
		
		String sql = "insert into table1 values('"+"','"+A_DATE+"','"+APPLICANT+"','"+NAME+"','"+MUCH+"','"+"待审核"+"','"+R_APPLICANT+"')";
		//访问数据库
//		String sql = "insert into table1 values('add','a','w','d','dd')";
		new visit_data(sql);
		//显示
		new show_data("table1", table);
	}
	//查询
	public void look(){
		String much = textField.getText();
		String time = textField_3.getText();
		String a = textField_1.getText();		
		String name = textField_4.getText();
		String ID= textField_5.getText();
		
		

		String [] Sb={much,name,time,a,ID};
		String [] Sb1={"MUCH","NAME","A_DATE","APPLICANT","NUMBE"};
		
		String sql="select * from table1 where 1 = 1";
		for (int i = 0; i < Sb.length; i++) {
			
			if(!Sb[i].equals("")){
				sql=sql+" and "+Sb1[i]+"='"+Sb[i]+"'";
			}
		}
		for(int i = 0 ; i < table.getRowCount();i++){
			for(int j = 0; j < table.getColumnCount();j++){
				table.setValueAt(null, i, j);
			}
		}
		show_data.look_data( sql, table);
	}
	public void newData() {
		for(int i = 0 ; i < table.getRowCount();i++){
			for(int j = 0; j < table.getColumnCount();j++){
				table.setValueAt(null, i, j);
			}
		}
		new show_data("table1",table);
	}
}

